﻿using System;
using System.Drawing;
using System.IO;
using System.Xml.Serialization;

namespace Medicalsystem.MCP.FormDesigner.Common
{
    [Serializable()]
    public class FontSerializer
    {
        public string Name = "宋体";
        public float Size = 9;
        public bool Bold = false;
        public bool Italic = false;
        public bool Strikeout = false;
        public bool Underline = false;
        public GraphicsUnit Unit = GraphicsUnit.Point;
        public Byte GdiCharSet = 134;
        public bool GdiVerticalFont = false;

        public FontSerializer()
        {
        }

        public string ToXml()
        {
            string xml = "";
            TextWriter tw = new StringWriter();

            XmlSerializer formatter = new XmlSerializer(typeof(FontSerializer));
            formatter.Serialize(tw, this);

            xml = tw.ToString();
            xml = xml.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");
            xml = xml.Replace(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");
            xml = xml.Replace("<FontSerializer>", "");
            xml = xml.Replace("</FontSerializer>", "");
            return xml;
        }

        public Font ToFont()
        {
            FontStyle fontStyle = FontStyle.Regular;
            if (this.Bold)
            {
                fontStyle = fontStyle | FontStyle.Bold;
            }

            if (this.Italic)
            {
                fontStyle = fontStyle | FontStyle.Italic;
            }

            if (this.Strikeout)
            {
                fontStyle = fontStyle | FontStyle.Strikeout;
            }

            if (this.Underline)
            {
                fontStyle = fontStyle | FontStyle.Underline;
            }

            Font font = new Font(this.Name, this.Size, fontStyle, this.Unit, this.GdiCharSet, this.GdiVerticalFont);
            return font;
        }

        public static FontSerializer FromFont(Font font)
        {
            if (font == null) return null;
            FontSerializer fc = new FontSerializer();
            fc.Name = font.Name;
            fc.Size = font.Size;
            fc.Bold = font.Bold;
            fc.Italic = font.Italic;
            fc.GdiCharSet = font.GdiCharSet;
            fc.GdiVerticalFont = font.GdiVerticalFont;
            fc.Unit = font.Unit;

            return fc;
        }

        public static FontSerializer FromXml(string xml)
        {
            xml = xml.Replace("<Font>", "<FontSerializer>");
            xml = xml.Replace("</Font>", "</FontSerializer>");
            XmlSerializer formatter = new XmlSerializer(typeof(FontSerializer));
            TextReader tr = new StringReader(xml);

            FontSerializer fc = null;
            try
            {
                fc = (FontSerializer)formatter.Deserialize(tr);
            }
            catch
            {
                fc = new FontSerializer();
            }

            return fc;
        }
    }
}